<?php
/**
 * @title 5534. 可见点的最大数目 - 中心角度
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[][] $points
     * @param Integer $angle
     * @param Integer[] $location
     * @return Integer
     */
    function visiblePoints($points, $angle, $location) {
        /**
         * @since 2020-10-10 角度
         */
        $angleArray = [];

        /**
         * @since 2020-10-10 原点数量
         */
        $originCount = 0;

        /**
         * @since 2020-10-10 坐标中心转移到location
         */
        foreach ($points as $key=>$point){
            $x = $point[0] - $location[0];
            $y = $point[1] - $location[1];

            /**
             * @since 2020-10-10 坐标轴
             */
            if($x == 0){
                if($y == 0){
                    /**
                     * @since 2020-10-10 在原点, 任意位置可以看
                     */
                    $originCount++;
                } elseif($y > 0) {
                    /**
                     * @since 2020-10-10 90°
                     */
                    $angleArray[] = 90;
                } else {
                    /**
                     * @since 2020-10-10 270°
                     */
                    $angleArray[] = 270;
                }
            } elseif($y == 0) {
                if($x > 0){
                    /**
                     * @since 2020-10-10 0°
                     */
                    $angleArray[] = 0;
                } else {
                    /**
                     * @since 2020-10-10 180°
                     */
                    $angleArray[] = 180;
                }
            } else {
                $tan = abs($y)/abs($x);
                $angleNum = rad2deg(atan($tan));

                /**
                 * @since 2020-10-10 第一象限
                 */
                if($x > 0 AND $y > 0){
                } elseif($x < 0 AND $y > 0) {
                    /**
                     * @since 2020-10-10 第二象限
                     */
                    $angleNum = 180-$angleNum;
                } elseif($x < 0 AND $y < 0){
                    /**
                     * @since 2020-10-10 第三象限
                     */
                    $angleNum = 180+$angleNum;
                } else {
                    /**
                     * @since 2020-10-10 第四象限
                     */
                    $angleNum = 360-$angleNum;
                }

                /**
                 * @since 2020-10-10 角度
                 */
                $angleArray[] = $angleNum;
            }
        }

        /**
         * @since 2020-10-10 角度排序
         */
        if(empty($angleArray)){
            return $originCount;
        } else {
            sort($angleArray);
        }
        return $angleArray;

        /**
         * @since 2020-10-10 每个角度依次作为可视角最边缘起点
         */
        $len = count($angleArray);
        $lastAngle = null;
        $max = 1;
        foreach ($angleArray as $key=>$a){
            /**
             * @since 2020-10-10 角度一样跳过
             */
            if($a === $lastAngle){
                continue;
            } else {
                $lastAngle = $a;
            }

            /**
             * @since 2020-10-10 往后找角度
             */
            for($j = $max; $j <= $len-1; $j++){
                $k = ($key+$j)%$len;
                $b = $angleArray[$k];

                /**
                 * @since 2020-10-10 a到b角度的夹角d
                 */
                if($b>=$a){
                    $d = $b-$a;
                } else {
                    $d = $b+360-$a;
                }

                /**
                 * @since 2020-10-10 夹角是否在范围内
                 */
                if($d <= $angle){
                    $max = $j+1;
                } else {
                    break;
                }
            }
        }

        /**
         * @return
         */
        return $max+$originCount;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202010/2020.10.04_3.php
 */
$datas = [
    [
        [[67859008,28411340],[961169753,481736104],[607164680,253818850],[189136257,154144926],[383780949,424090561],[121627525,204120793],[394219318,264811656],[578057842,465509550],[790273581,67478300],[58262563,265534873],[942678096,667639121],[938807615,849288440],[574209003,546177284],[870876415,741827035],[343045623,300134135],[925879621,438000010],[517160984,734451296],[472306301,334167788],[379698393,574002298],[545973139,158657472],[242155665,662106393],[167707086,515531223],[56992257,181906772],[45674024,397165777],[651338405,333525912],[508565320,906073477],[176512048,807948281],[247972329,525137162],[410715669,933233175],[149752540,257530712],[365695251,342789890],[660615900,160840554],[32269680,933844118],[151759972,285544313],[79681763,598168689],[265785679,103217572],[97830862,782534265],[866437168,687742795],[271079978,32077829],[692328190,38235111],[934285081,339761236],[46226560,555364927],[26895834,673716166],[837197899,414576961],[400391710,18039667],[32005957,646684521],[518618483,61068473],[565339285,242671372],[173275294,517947747],[199517934,841523565],[513891763,811102566],[830750960,415114793],[691642665,345503385],[129799692,133450932],[313615138,86142459],[58559370,477521225],[664434652,298574533],[880471652,628664808],[442529874,751150296],[523170641,761648449],[630527807,289822516],[179832362,99778565],[884864570,578668371],[706735043,499554667],[8976817,156866896],[788997339,639782004],[421100744,34696333],[125320289,470681194],[503236477,218411314],[358628498,305056153],[67976605,19365856],[121477645,380114805],[385257067,646937958],[391485951,701853564],[386826659,703014670],[896497733,277250317],[663643151,101197040],[557546236,95718637],[44355575,51274052],[786531797,83327703],[890851913,739932116],[157174834,104849520],[365937660,130540465],[187869820,964161193],[259908308,583279192],[248424061,331796443],[546764617,928746142],[872837953,406962247],[539755991,164847302],[647522862,207575960],[125156970,453930731],[537578274,202621386],[532116369,753345174],[355544980,867077973],[501025688,656792564]],
        49,
        [284212071,845234541],
    ],
    // [
    //     [[1,1],[2,2],[3,3],[4,4],[1,2],[2,1]], 0, [1,1]
    // ],
    // [
    //     [[2,1],[2,2],[3,3]], 90, [1,1]
    // ],
    // [
    //     [[2,1],[2,2],[3,4],[1,1]], 90, [1,1]
    // ],
    // [
    //     [[1,0],[2,1]], 13, [1,1],
    // ],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    // var_dump($data);

    $obj = new Solution();
    $result = $obj->visiblePoints(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
